//出厂测试 程序

#include <stdint.h>
#include "FactoryTest.h"
#include "LCD/HT162X_Pin.h"
#include "LCD/HT162X_Parts_Num.h"
#include "main.h"


uint16_t Factory_Number = 0;
uint8_t Factory_Enable = 0;
uint8_t Factory_LCD_ONOFF = 1;

void Factory_EncoderKey_Handler(void)
{
    static uint8_t seq = 0;

    Buzzer_Encoder_Down();
    if(seq == 0)
    {
        seq = 1;
        ExhaustFanOpen;
        IO_Lcd_ON_OFF_H;
        Factory_LCD_ONOFF = 1;
    }
    else if(seq == 1)
    {
        seq = 2;
        IgniterOpen;
    }
    else if(seq == 2)
    {
        seq = 3;
        HeaterFanOpen;
    }
    else if(seq == 3)
    {
        seq = 4;
        HeaterFanClose;
    }
    else if(seq == 4)
    {
        seq = 5;
        IgniterClose;
    }
    else if(seq == 5)
    {
        seq = 6;
        ExhaustFanClose;
    }
    else
    {
        seq = 0;
        IO_Lcd_ON_OFF_L;
        Factory_LCD_ONOFF = 0;
    }
}
void Factory_EncoderLongKey_Handler(void)
{
    Buzzer_Alarm();
    ESP_LOGI("FactoryTest", "Factory_EncoderLongKey_Handler");

    if(UI_System.Grill_Unit == Unit_F) UI_SetUnit(Unit_C);
    else UI_SetUnit(Unit_F);
}
void Factory_EncoderTurnR_Handler(void)
{
    Buzzer_Encoder_Down();
    if(Factory_Number < 999) Factory_Number += 111;
}
void Factory_EncoderTrunL_Handler(void)
{
    Buzzer_Encoder_Down();
    if(Factory_Number > 0) Factory_Number -= 111;
}

void Delay_16ms(void)
{
    uint16_t i;

    for (i = 0; i < 2500 * 16; i++)
    {
        
    }
}

void Factory_Main(void)
{
    uint16_t i;

    //上电后,检测菜单键有效
    if (GPIO_ReadEncoderKey == 0x00)
    {
        Delay_16ms();
        Delay_16ms();
        Delay_16ms();
        if (GPIO_ReadEncoderKey == 0x00)
        {
            Delay_16ms();
            Delay_16ms();
            Delay_16ms();
            if (GPIO_ReadEncoderKey == 0x00)
            {
                HeaterFanClose;
                IgniterClose;
                ExhaustFanClose;
                //编码器处理初始化
                Encoder_Key_Handler = Factory_EncoderKey_Handler;
                Encoder_LongKey_Handler = Factory_EncoderLongKey_Handler;
                Encoder_TurnR_Handler = Factory_EncoderTurnR_Handler;
                Encoder_TurnL_Handler = Factory_EncoderTrunL_Handler;

                UI_System.ICO_WIFI = UI_ON;
                UI_System.ICO_Bluetooth = UI_ON;
                UI_System.ICO_Auger = UI_ON;
                UI_System.ICO_Fan = UI_ON;
                UI_System.ICO_Heating = UI_ON;
                UI_System.ICO_Error = UI_ON;
                UI_System.ICO_Cell = UI_ON;
                UI_System.Shine_Colon = UI_ON;
                UI_System.Shine_WIFI = UI_ON;
                UI_System.Grill_Unit = Unit_F;
                //打开WIFI AP模块功能,专用于出厂检测WIFI信号
                wifi_softap_init();
                
                while (1)
                {
                    Factory_Enable = 1;
                    UI_System.RTD.Settings = Factory_Number;
                    vTaskDelay(pdMS_TO_TICKS(20));
                    EncoderMain();

                    Lcd_Set(Char_ON);
                    Lcd_Motor(Char_ON);
                    Lcd_Fan(Char_ON);
                    Lcd_HotRod(Char_ON);
                    Lcd_Wifi(Char_ON);
                    Lcd_Blue(Char_ON);
                    Lcd_Timer_Title(Char_ON);
                    Lcd_Timer_Colon(Char_ON);
                    Lcd_P1(Char_ON);
                    Lcd_P2(Char_ON);
                    Lcd_Error(Char_ON);
                    Lcd_Cell(Char_ON);
                    Lcd_Act(Char_ON);
                    Lcd_Fire(Char_ON);
                    Lcd_Line(Char_ON);
                    Lcd_NumShow(UI_Timer_Hour,Factory_Number / 10);
                    Lcd_NumShow(UI_Timer_Minute,Factory_Number / 10);

                    Lcd_NumShow(UI_RTDSettings,Factory_Number);
                    if(Temp_Param.oven_Err == Pt_Normal) Lcd_Numbers(UI_System.RTD.Actual,NULL,TempAct_seg1,TempAct_seg2,TempAct_seg3);
                    else Lcd_Numbers(Factory_Number,NULL,TempAct_seg1,TempAct_seg2,TempAct_seg3);
                    if(Temp_Param.probe1_Err == Pt_Normal) Lcd_Numbers(UI_System.ProbeA.Actual,NULL,P1_seg1,P1_seg2,P1_seg3);
                    else Lcd_Numbers(Factory_Number,NULL,P1_seg1,P1_seg2,P1_seg3);
                    if(Temp_Param.probe2_Err == Pt_Normal) Lcd_Numbers(UI_System.ProbeB.Actual,NULL,P2_seg1,P2_seg2,P2_seg3);
                    else Lcd_Numbers(Factory_Number,NULL,P2_seg1,P2_seg2,P2_seg3);
                    esp_task_wdt_reset();

                    if(Factory_LCD_ONOFF == 0) HT162X_RAM_Reset();
                    HT162X_RAM_Tx();
                }
            }
        }
    }
}
